<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@include file="/include/common/gridbackhead.jsp"%>
<%!
private class PageAction extends BaseAction{
	ResultSet rs = null;
	Map lastMap = null;
	String firstSn = "";
	String firstLastsn = "";
	DecimalFormat df = null;
	
	//准备action的环境变量 每次请次都会执行此方法 可在此方法中初如化一些变量
	protected void prepareActionEnv() throws Exception{
		df = new DecimalFormat("00");
	}
	
	protected void EndProcess() throws Exception{
		form.spsv("firstSn", firstSn);
		form.spsv("firstLastsn", firstLastsn);
	}
	
	//加載頁面默認數據
	protected boolean onLoadDefault() throws Exception{
		onSearchRetrieveResult();
		return true ;
	}
	
	// 取出查询結果
	protected void onSearchRetrieveResult() throws Exception{
		String sql = onSearchGetSql();
		if(sql == null){ret.setErrorInfo("查询语句异常,请联系资讯部!");return;}
		rs = utils.executeQuery("pan",sql);
		BaseDataEntities rows = new BaseDataEntities();
		while(rs.next()){
			BaseDataEntity subRow = new BaseDataEntity();
			subRow.FromResultSet(rs,false);
			if(subRow.isNullOrEmpty("src")){
				subRow.RemoveParam("src");
			}
			subRow.spsv("name", df.format(subRow.gpdv("srt"))+" "+subRow.gpsv("name"));
			if(subRow.gpiv("enab") == 0){
				subRow.spsv("name", subRow.gpsv("name")+"(已取消)");
			}
			subRow.RemoveParam("enab");
			subRow.RemoveParam("srt");
			
			BaseDataEntities childrens = this.getChildrens(subRow.gpsv("sn"));
			if(childrens != null && childrens.size() >0){
				subRow.spov("children", childrens);
			}
			rows.add(subRow);
			
			if(rows.size() == 1){
				firstSn = subRow.gpsv("sn");
				firstLastsn = subRow.gpsv("lastsn");
			}
		}
		ret.SetSearchResult(rows);
		getLastMap().clear();
	}

	//取出查詢語句
	protected String onSearchGetSql() throws Exception{
		StringBuilderEx sql =  new StringBuilderEx("select sn,lastsn,name,src,srt,[open],[enab] from pan.dbo.MENU where   lastsn = 0 ");
		sql.Append(" order by srt");
		return sql.toString();
	}
	
	protected BaseDataEntities getChildrens(String sn)  throws Exception{
		BaseDataEntities subRows = (BaseDataEntities)this.getLastMap().get(sn);
		if(subRows != null && subRows.size()>0){
			for (int i=0;i<subRows.size();i++){
				BaseDataEntity subRow = (BaseDataEntity)subRows.get(i);
				BaseDataEntities childrens = this.getChildrens(subRow.gpsv("sn"));
				if(childrens != null && childrens.size() >0){
					subRow.spov("children", childrens);
				}	
			}
		}
		return subRows;
	}	
	
	protected Map getLastMap() throws Exception{
		if(lastMap !=null){
			return lastMap;
		}
		lastMap = new HashMap();
		rs = utils.executeQuery("pan", "select sn,lastsn,name,src,srt,[open],[enab] from pan.dbo.MENU  order by lastsn,srt");
		while(rs.next()){
			String lastsn = utils.getRsString(rs, "lastsn");
			BaseDataEntities rows = null;
			if(lastMap.containsKey(lastsn)){
				rows = (BaseDataEntities)lastMap.get(lastsn);
			}else{
				rows = new BaseDataEntities();
				lastMap.put(lastsn, rows);
			}
			BaseDataEntity row = new BaseDataEntity();
			row.FromResultSet(rs, false);
			row.spsv("name", df.format(row.gpdv("srt"))+" "+row.gpsv("name"));
			if(row.gpiv("enab") == 0){
				row.spsv("name", row.gpsv("name")+"(已取消)");
			}
			
			if(row.isNullOrEmpty("src")){
				row.RemoveParam("src");
			}
			row.RemoveParam("enab");
			row.RemoveParam("srt");
			rows.add(row);
		}
		return lastMap;
	}
}
%>
<%@include file="/include/common/gridbackend.jsp"%>
